import { Message } from '@teamix/ui';
import { NameSpace } from 'utils/index';
import exceed from 'utils/apimap';
import intl from '../../../../../../../../../locale';

const ns = NameSpace('deploykey');
export const RECEIVE_REPOSITORY_DEPLOY_KEYS = ns('RECEIVE_REPOSITORY_DEPLOY_KEYS');
export const RECEIVE_AUTHORIZED_DEPLOY_KEYS = ns('RECEIVE_AUTHORIZED_DEPLOY_KEYS');
export const RECEIVE_IP_WHITE_CHECK = ns('RECEIVE_IP_WHITE_CHECK');
export const UPDATE_DATA = ns('UPDATE_DATA');

const { pathResource, organization } = window.FORCE;

export function updateData(data) {
  return (dispatch) => {
    dispatch({
      type: UPDATE_DATA,
      data,
    });
  };
}

export function getRepositoryDeployKeys() {
  return (dispatch) => {
    exceed
      .forceFetch({
        api: 'project.enabled.deploy_keys',
        params: {
          id: pathResource.id,
        },

        data: {
          per_page: 100,
        },
      })
      .then((result) => {
        dispatch({
          data: result,
          type: RECEIVE_REPOSITORY_DEPLOY_KEYS,
        });
      });
  };
}

export function create(formData, cb) {
  return (dispatch) => {
    exceed
      .fetch({
        api: 'project.create.deploy_keys',
        params: {
          id: pathResource.id,
        },

        data: formData,
      })
      .then(() => {
        Message.success({
          title: intl.get({
            id: 'code-assets.settingsDeploykeys.actions.KeyCreated',
            defaultMessage: '密钥新建成功',
          }),
        });

        dispatch(getRepositoryDeployKeys());
        cb && cb();
      })
      .fail(() => {
        // 接口报错也要回调，以让按钮取消loading状态
        cb && cb('isFail');
      });
  };
}

export function getAuthorizedDeployKeys(exclude = []) {
  return (dispatch) => {
    exceed
      .forceFetch({
        api: 'authorized.deploy_keys',
        data: {
          exclude: exclude.join(),
          per_page: 100,
        },
      })
      .then((result) => {
        dispatch({
          type: RECEIVE_AUTHORIZED_DEPLOY_KEYS,
          data: result,
        });
      });
  };
}

export function disable(keyId, cb) {
  return (dispatch) => {
    exceed
      .forceFetch({
        api: 'projects.deploy_Keys.disable',
        params: {
          projectId: pathResource.id,
          keyId,
        },
      })
      .then(() => {
        Message.success({
          title: intl.get({
            id: 'code-assets.settingsDeploykeys.actions.TheKeyHasBeenRemoved',
            defaultMessage: '密钥移除成功',
          }),
        });

        dispatch(getRepositoryDeployKeys());
        dispatch(getAuthorizedDeployKeys([pathResource.id]));
        cb && cb();
      });
  };
}

export function remove(keyId, cb) {
  return (dispatch) => {
    exceed
      .forceFetch({
        api: 'projects.deploy_Keys.delete',
        params: {
          projectId: pathResource.id,
          keyId,
        },
      })
      .then(() => {
        Message.success({
          title: intl.get({
            id: 'code-assets.settingsDeploykeys.actions.KeyDeleted',
            defaultMessage: '密钥删除成功',
          }),
        });

        dispatch(getRepositoryDeployKeys());
        dispatch(getAuthorizedDeployKeys([pathResource.id]));
        cb && cb();
      });
  };
}

export function enable(id) {
  return (dispatch) => {
    exceed
      .forceFetch({
        api: 'enable.project.deploy_key',
        params: {
          projectId: pathResource.id,
          keyId: id,
        },
      })
      .then(() => {
        Message.success({
          title: intl.get({
            id: 'code-assets.settingsDeploykeys.actions.KeyEnabled',
            defaultMessage: '密钥启用成功',
          }),
        });

        dispatch(getRepositoryDeployKeys());
        dispatch(getAuthorizedDeployKeys([pathResource.id]));
      });
  };
}

export function getIpWhiteList() {
  return (dispatch) => {
    exceed
      .forceFetch({
        api: 'organization.ip_setting',
        params: {
          id: organization.id,
        },
      })
      .then((result) => {
        dispatch({
          type: RECEIVE_IP_WHITE_CHECK,
          data: result,
        });
      });
  };
}
